﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DTO;
using System.Data;

namespace DAO
{
    public class LoaiSachDAO
    {
        DataProvider provider;

        public LoaiSachDAO()
        {
            provider = new DataProvider();
        }

        public int ThemLoaiSach(LoaiSachDTO loaiSach)
        {

            string sqlStatement = "INSERT INTO LoaiSach(TenLoaiSach, DaXoa) VALUES (N'" + loaiSach.TenLoaiSach + "', 0)";

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public int CapNhatLoaiSach(LoaiSachDTO loaiSach)
        {
            string sqlStatement = "UPDATE LoaiSach SET TenLoaiSach=N'" + loaiSach.TenLoaiSach + "' WHERE MaLoaiSach=" + loaiSach.MaLoaiSach;
            
            return provider.ExecuteNonQuery(sqlStatement);
        }
                
        public int XoaLoaiSach(LoaiSachDTO loaiSach)
        {
            string sqlStatement = "UPDATE LoaiSach SET DaXoa=1 WHERE MaLoaiSach=" + loaiSach.MaLoaiSach;

            return provider.ExecuteNonQuery(sqlStatement);
        }

        public ArrayList LayDanhSachLoaiSach()
        {
            ArrayList arrLoaiSach = new ArrayList();
            try
            {
                string query = "SELECT * FROM LoaiSach WHERE DaXoa=0";
                DataTable dt = new DataTable();
                dt = provider.ExecuteQuery(query);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    LoaiSachDTO item = new LoaiSachDTO();

                    item.MaLoaiSach = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    item.TenLoaiSach = dt.Rows[i].ItemArray[1].ToString();
                    item.DaXoa = bool.Parse(dt.Rows[i].ItemArray[2].ToString());

                    arrLoaiSach.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message);
            }
            return arrLoaiSach;
        }
    }
}
